package com.xb.cloud.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.xb.cloud.service.client.ServiceReturnCodes;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.user.model.EndUserAutoLogin;
import com.xb.cloud.user.model.EndUserAutoLoginExample;
import com.xb.cloud.user.model.mapper.EndUserAutoLoginMapper;
import com.xb.cloud.user.service.client.EndUserAutoLoginService;
import com.xb.cloud.user.service.client.bo.EndUserAutoLoginBo;

@Service
public class EndUserAutoLoginServiceImpl extends AbstractService implements EndUserAutoLoginService {

	@Resource
	EndUserAutoLoginMapper mapper;

	@Override
	public ServiceReturnInfo createEndUserAutoLogin(EndUserAutoLoginBo endUserAutoLoginBO) {
		if (endUserAutoLoginBO == null) {
			return ServiceReturnUtils.error(ServiceReturnCodes.PARAM_IS_NULL);
		}
		EndUserAutoLogin endUserAutoLogin = new EndUserAutoLogin();
		BeanUtils.copyProperties(endUserAutoLoginBO, endUserAutoLogin);

		endUserAutoLogin.setCreateTime(timeService.getDate());
		mapper.insertSelective(endUserAutoLogin);
		return ServiceReturnUtils.success();
	}

	@Override
	public ServiceReturnInfo getUserAutoLoginByEndUserId(Long endUserId) {
		EndUserAutoLogin endUserAutoLogin = mapper.selectByPrimaryKey(endUserId);
		if (endUserAutoLogin == null) {
			return ServiceReturnUtils.success(null);
		}
		EndUserAutoLoginBo bo = new EndUserAutoLoginBo();
		BeanUtils.copyProperties(endUserAutoLogin, bo);
		return ServiceReturnUtils.success(bo);
	}

	@Override
	public ServiceReturnInfo getByAut(String autoUserToken) {
		EndUserAutoLoginBo bo = new EndUserAutoLoginBo();
		EndUserAutoLoginExample example = new EndUserAutoLoginExample();
		example.createCriteria().andAutEqualTo(autoUserToken);
		List<EndUserAutoLogin> autoLoginList = mapper.selectByExample(example);
		if (CollectionUtils.isEmpty(autoLoginList)) {
			return ServiceReturnUtils.success(null);
		} else {
			BeanUtils.copyProperties(autoLoginList.get(0), bo);
		}
		return ServiceReturnUtils.success(bo);
	}

	@Override
	public ServiceReturnInfo deleteAutoLogin(EndUserAutoLoginBo bo) {
		EndUserAutoLoginExample example = new EndUserAutoLoginExample();
		example.createCriteria().andAutEqualTo(bo.getAut()).andEndUserIdEqualTo(bo.getEndUserId())
				.andCompanyIdEqualTo(bo.getCompanyId());
		mapper.deleteByExample(example);
		return ServiceReturnUtils.success();
	}

	@Override
	public ServiceReturnInfo deleteByAut(String aut) {
		EndUserAutoLoginExample example = new EndUserAutoLoginExample();
		example.createCriteria().andAutEqualTo(aut);
		mapper.deleteByExample(example);
		return ServiceReturnUtils.success();
	}

}
